(function () {
    // const basePath = "http://10.133.9.14/im-fe/";
    const basePath = "http://10.133.10.39/im-fe/";
    // const basePath = "http://10.133.120.16/im-fe/";
    // const basePath = "http://localhost:8080/";

    let os = function () {
        var ua = navigator.userAgent,
            isWindowsPhone = /(?:Windows Phone)/.test(ua),
            isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
            isAndroid = /(?:Android)/.test(ua),
            isFireFox = /(?:Firefox)/.test(ua),
            isChrome = /(?:Chrome|CriOS)/.test(ua),
            isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),
            isPhone = /(?:iPhone)/.test(ua) && !isTablet,
            isPc = !isPhone && !isAndroid && !isSymbian;
        return {
            isTablet: isTablet,
            isPhone: isPhone,
            isAndroid: isAndroid,
            isPc: isPc
        };
    }();

    function themeColor(color) {
        JQ("#chat_online_top").css('background', color)
    }

    function loadOnlineCustomerScript(sid, applicationNo, sign, hideCallback, showCallback, color) {
        window.JQ = window.$;
        if (window.$ && !window.$.ajax) {
            // 閬垮厤宸叉湁$鍐茬獊锛屼絾闈瀓query
            window.s$ = window.$;
        }

        if (!window.JQ || window.s$) {
            // const response = await fetch(basePath + "scripts/jquery.min.js");
            // const script = await response.text();
            // eval(script);
            document.querySelector('body').appendChild(((ele) => {
                ele.src = basePath + "scripts/jquery.min.js";
                return ele;
            })(document.createElement('script')));

            let inter = setInterval(function () {
                if (!window.$.ajax) {
                    return;
                }
                clearInterval(inter);
                window.JQ = window.$;
                if (window.s$) {
                    window.$ = window.s$;
                }
                init(hideCallback, showCallback, color);
            }, 500);
        } else {
            init(hideCallback, showCallback, color);
        }

        function init(hideCallback, showCallback, color) {
            let param = {
                tag: true,
                num: 1,
                ele: document.createElement('div'),
                icon: '&#xe6a2;',
                homeIcon: '&#xe643;',
                moreIcon: '<img id="online_chat" src="' + basePath + '/img/kefu.png" title="鍦ㄧ嚎瀹㈡湇" style="width: 30px;height: 30px;vertical-align: middle;">',
                personalIcon: '&#xe644;',
                closeIcon: '&#xe628;',
                distance: '500'
            };
            let param2 = {
                tag: true,
                num: 1,
                ele: document.createElement('div'),
                icon: '&#xe6a2;',
                homeIcon: '&#xe643;',
                returnIcon: '<img id="close_online_chat" src="' + basePath + '/img/return.png" title="杩斿洖" style="width: 20px;height: 20px;vertical-align: middle;">',
                personalIcon: '&#xe644;',
                closeIcon: '&#xe628;',
                distance: '500'
            };

            JQ("body").append('<link href="' + basePath + '/css/mobile.min.css" rel="stylesheet"/>');

            document.querySelector('body').appendChild(((ele) => {
                ele.id = 'chat_robot_tip';
                ele.innerHTML = param.moreIcon;
                return ele;
            })(param.ele));
            addIconWinEvent();


            document.querySelector('body').appendChild(((ele) => {
                ele.id = 'return_button';
                ele.innerHTML = param2.returnIcon;
                return ele;
            })(param2.ele));
            JQ("#return_button").hide();

            JQ("#close_online_chat").click(function () {
                if (JQ("#chat_online_main").is(":visible")) {
                    JQ("#chat_online_main").hide();
                    JQ("#chat_robot_tip").show();
                    JQ("#return_button").hide();
                    showCallback()
                } else {
                    JQ("#chat_online_main").show();
                    JQ("#chat_robot_tip").hide();
                    JQ("#return_button").show();
                    hideCallback()
                }
            })
        }

        function addIconWinEvent() {
            //鎮诞绐楃殑鎷栧姩
            let bool = false,
                offsetX = 0,
                offsetY = 0;
            beginX = 0;beginY = 0;
            endX = 0;endY = 0
            JQ("#chat_robot_tip").mousedown(function (e) {
                bool = true;
                offsetX = e.offsetX;
                offsetY = e.offsetY;
                JQ("#chat_robot_tip").css('cursor', 'move');
                beginX = e.clientX - offsetX;
                beginY = e.clientY - offsetY;

                return false;
            })
            JQ(document).mouseup(function (e) {
                bool = false;
                endX = e.clientX - offsetX;
                endY = e.clientY - offsetY;
            });

            JQ(document).mousemove(function (e) {
                if (!bool) {
                    return;
                }
                let x = e.clientX - offsetX,
                    y = e.clientY - offsetY;
                $("#chat_robot_tip").css("left", x).css("top", y);
                return false;
            });
            JQ("#chat_robot_tip").click(function () {
                if(beginX !=endX || beginY != endY){
                    return;
                }
                if (JQ("#chat_online_main").length == 0) {
                    $.ajax({
                        // url: "http://localhost:8818/xyocs/api/sys/user/getIMTokenForFront",
                        // url: "http://10.133.9.14/xyocs/api/sys/user/getIMTokenForFront",
                        // url: "http://10.133.120.16/xyocs/api/sys/user/getIMTokenForFront",
                        url: "http://10.133.10.39/xyocs/api/sys/user/getIMTokenForFront",
                        type: 'post',
                        data: JSON.stringify({"sid": sid, "signture": sign, "applicationNo": applicationNo}),
                        dataType: "json",
                        contentType: 'application/json',

                        success: function (data) {
                            console.log(data);
                            // let iframeUrl = data.data.replace("http://10.133.9.14/im-fe/", "http://localhost:8080/");
                            let iframeUrl = data.data;
                            let style = "";
                            if (os.isPc) {
                                let clientWidth = document.documentElement.clientWidth,
                                    clientHeight = document.documentElement.clientHeight,
                                    objWidth = 500,
                                    objHeight = 400,
                                    x = 20,
                                    y = (clientHeight - objHeight) / 4;
                                style = 'style="left:' + x + '%;top:' + y + 'px;"';
                            }
                            JQ("body").append('<div id="chat_online_main" ' + style + ' class="chat_online_show"><div id="chat_online_top"><div id="chat_online_title">鍦ㄧ嚎瀹㈡湇</div><img id="chat_online_close" title="close" src="' + basePath + '/img/close.jpg" alt="close"/></div><div id="chat_online_content"><iframe id="chat_online_iframe" src="' + iframeUrl + '" style="width: 100%;height: 100%;border: 0;" /></div></div>')
                            style ? JQ("#chat_online_title").width(JQ("#chat_online_top").width() * 0.95) : void 0;
                            if (color !== null && color !== undefined && color !== '') {
                                JQ("#chat_online_top").css('background', color)
                            }
                            addChatWinEvent();
                            if (!os.isPc) {
                                JQ("#chat_robot_tip").hide();
                                JQ("#return_button").show();
                            }
                            hideCallback();
                        },
                        error: function (data) {
                            console.log(data);
                        }
                    })

                } else if (JQ("#chat_online_main").is(":visible")) {
                    JQ("#chat_online_main").hide();
                    if (!os.isPc) {
                        JQ("#chat_robot_tip").show();
                        JQ("#return_button").hide();
                    }

                    showCallback()
                } else {
                    JQ("#chat_online_main").show();
                    if (!os.isPc) {
                        JQ("#chat_robot_tip").hide();
                        JQ("#return_button").show();
                    }
                    hideCallback()
                }
            });
        }

        function addChatWinEvent() {
            //绐楀彛鐨勬嫋鍔�
            let bool = false,
                offsetX = 0,
                offsetY = 0;
            JQ("#chat_online_title").mousedown(function (e) {
                bool = true;
                offsetX = e.offsetX;
                offsetY = e.offsetY;
                JQ("#chat_online_title").css('cursor', 'move');
            })
            JQ(document).mouseup(function () {

                bool = false;
            });

            JQ("#chat_online_close").click(function () {
                JQ("#chat_online_main").hide();
            });

            JQ(document).mousemove(function (e) {
                if (!bool) {
                    return;
                }
                let x = e.clientX - offsetX,
                    y = e.clientY - offsetY;
                $("#chat_online_main").css("left", x).css("top", y);
                return false;
            });

        }

        history.pushState(null,null, document.URL);
        window.addEventListener("popstate", function(e) {
            if (JQ("#chat_online_main").is(":visible")) {
                JQ("#chat_online_main").hide();
                JQ("#chat_robot_tip").show();
                JQ("#return_button").hide();
                showCallback();
                history.pushState(null,null, document.URL)
            }
        }, false);

    };
    window.loadOnlineCustomerScript = loadOnlineCustomerScript;
}())